<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example C&#43;&#43; function - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../experimental/c-api/limitations.html"><img src="../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../experimental/c-api.html"><img src="../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../experimental/c-api/example2.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">Example C++ function</h1></div></div></div>
  <p>Let us start with a simple C++ function which we wish to make available to C code:</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">namespace</span> <span class="n">outcome_e</span> <span class="o">=</span> <span class="n">BOOST_OUTCOME_V2_NAMESPACE</span><span class="o">::</span><span class="n">experimental</span><span class="p">;</span>

<span class="c1">// Fill the supplied buffer with the integer v converted to a string,
</span><span class="c1">// returning length of string minus null terminator
</span><span class="c1"></span><span class="n">outcome_e</span><span class="o">::</span><span class="n">status_result</span><span class="o">&lt;</span><span class="n">size_t</span><span class="o">&gt;</span> <span class="n">to_string</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">buffer</span><span class="p">,</span> <span class="n">size_t</span> <span class="n">bufferlen</span><span class="p">,</span> <span class="kt">int</span> <span class="n">v</span><span class="p">)</span> <span class="k">noexcept</span>
<span class="p">{</span>
  <span class="k">try</span>
  <span class="p">{</span>
    <span class="c1">// Could throw an exception!
</span><span class="c1"></span>    <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">temp</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">to_string</span><span class="p">(</span><span class="n">v</span><span class="p">));</span>

    <span class="c1">// Will this string exceed the supplied buffer?
</span><span class="c1"></span>    <span class="k">if</span><span class="p">(</span><span class="n">temp</span><span class="p">.</span><span class="n">size</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&gt;</span> <span class="n">bufferlen</span><span class="p">)</span>
      <span class="k">return</span> <span class="n">outcome_e</span><span class="o">::</span><span class="n">errc</span><span class="o">::</span><span class="n">no_buffer_space</span><span class="p">;</span>

    <span class="c1">// Copy the string into the supplied buffer, and return length of string
</span><span class="c1"></span>    <span class="n">memcpy</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="n">temp</span><span class="p">.</span><span class="n">data</span><span class="p">(),</span> <span class="n">temp</span><span class="p">.</span><span class="n">size</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
    <span class="k">return</span> <span class="n">temp</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
  <span class="p">}</span>
  <span class="k">catch</span><span class="p">(...)</span>
  <span class="p">{</span>
    <span class="c1">// This is the &lt;system_error2&gt; analogue of Standard Outcome&#39;s
</span><span class="c1"></span>    <span class="c1">// error_from_exception() utility function. It consumes an exception
</span><span class="c1"></span>    <span class="c1">// ptr (defaulted to current exception), and tries to match it to a
</span><span class="c1"></span>    <span class="c1">// standard C++ library exception type, returning a system_code
</span><span class="c1"></span>    <span class="c1">// with an appropriate code domain (generic_code, posix_code,
</span><span class="c1"></span>    <span class="c1">// win32_code).
</span><span class="c1"></span>    <span class="c1">//
</span><span class="c1"></span>    <span class="c1">// Note that using this function requires including
</span><span class="c1"></span>    <span class="c1">// &lt;boost/outcome/experimental/status-code/system_code_from_exception.hpp&gt;
</span><span class="c1"></span>    <span class="c1">// It is NOT included by Experimental Outcome by default.
</span><span class="c1"></span>    <span class="k">return</span> <span class="n">outcome_e</span><span class="o">::</span><span class="n">system_code_from_exception</span><span class="p">();</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/cpp_api.cpp#L37" class="code-snippet-url" target="_blank">View this code on Github</a></div>


<p>As the alias <code>status_result&lt;size_t&gt;</code> defaults the erased type to the alias <code>system_code</code>,
the <code>to_string()</code> function returns (in concrete types) <code>basic_result&lt;size_t, status_code&lt;erased&lt;intptr_t&gt;&gt;&gt;</code>.</p>

<p>The standard Outcome function referenced is documented at

<a href="https://en.cppreference.com/w/cpp/error/error_code" class="api-reference" target="_blank"><i class="fa fa-book" aria-hidden="true"></i> <code>std::error_code error_from_exception(std::exception_ptr &amp;&amp;ep = std::current_exception(), std::error_code not_matched = std::make_error_code(std::errc::resource_unavailable_try_again)) noexcept</code></a>
.
The proposed <code>&lt;system_error2&gt;</code> reference library implementation provides an identically named
function taking similar parameters, but it returns a <code>outcome_e::system_code</code> (<code>status_code&lt;erased&lt;intptr_t&gt;&gt;</code>) instead of a <code>std::error_code</code>.</p>


        </div><p><small>Last revised: February 05, 2019 at 17:14:18 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../experimental/c-api/limitations.html"><img src="../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../experimental/c-api.html"><img src="../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../experimental/c-api/example2.html"><img src="../../images/next.png" alt="Next"></a></div></body>
</html>
